<template>
	
	<view>
		
		<Barcode v-if="barcodeshow" :opations="opations">暂无条形码</Barcode>
		<GetUserInfoChild v-model="popInfoShow" @send="getSonValue"></GetUserInfoChild>
		
		<button type="default"  @click="showinfopop">授权头像昵称 <image class="userpic" :src="avatarUrl" mode="aspectFill"></image> {{nickName}} </button>
		<button type="default" @click="to_subpack">跳转分包页面</button>
		
	</view>


</template>

<script>
	import GetUserInfoChild from '@/components//GetUserInfo/GetUserInfoChild.vue'
	import Barcode from "@/components/Barcode/Barcode.vue"

	export default {
		components: {
			GetUserInfoChild,
			Barcode
		},
		data() {
			return {
				nickName:'',
				avatarUrl:'',
				popInfoShow: false,
				
				barcodeshow: false, //等待接口返回完毕再加载条形码
				opations: {
					width: 3,
					height: 48,
					textMargin: 4,
					fontSize: 20,
					fontColor: '#000',
					lineColor: '#000',
					background: 'transparent',
					margin: 0,
					marginBottom: 3,
				},

			}
		},
		
		
		methods: {
		
			showinfopop() {
				this.popInfoShow = true; //弹出pop
			},
			getSonValue(res) {
				console.log(res)
				uni.setStorageSync('userInfo', res);
				this.nickName = res.nickName;
				this.avatarUrl = res.avatarUrl;
				// this.$emit('getUserInfoFn',res);
				//将昵称和头像存在本地，然后组件加载时候判断先从本地取
			},

			to_subpack(){
				uni.navigateTo({
					url: '../subpack/success',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			
			/**
			 * 加载条形码
			 */
			getBarcode(openid){
				var that = this
				let params = {
					openId: openid,
					status:2,
					nickname:'',
					headUrl: ''
				}
				this.$api.getBarcode(params).then(res => {
					let result = res.data.data;
					that.barcodeshow = true;
					that.$set(that.opations, "text", result.couponCode); //赋值code
				})
				.catch(err => {
					this.$u.toast(err);
				});
			},
			
			/**
			 * 获取openid
			 */
			getOpenid() {
				var that = this;
				this.$api.openid().then(res=>{
					let openid = res;
					console.log(openid);
					that.getBarcode(openid)
				}).catch(err=>{
					this.$u.toast(err);
				})
			},

		},
		
		mounted(){
			this.getOpenid();
		},
		
	}
</script>

<style lang="scss" scoped>
	.userpic{
		width: 20px;
		height: 20px;
	}
</style>